package com.zhihuishu.treenity.controller.course;


import com.zhihuishu.micro.course.openapi.course.CCFolderOpenService;
import com.zhihuishu.micro.course.openapi.course.dto.CourseFolderDto;
import com.zhihuishu.micro.course.openapi.course.dto.CourseFolderTreeDto;
import com.zhihuishu.remote.RemoteException;
import com.zhihuishu.remote.RemoteResult;
import com.zhihuishu.treenity.controller.BaseController;
import com.zhihuishu.treenity.log.LoggerCollectionEnum;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/***
 * @author jiangli
 *
 */
@Controller
@RequestMapping("/course/thirdStep/dir")
public class ThirdStepDirController extends BaseController {
    private static final String MODULE_NAME_CN = "文件夹";
    private static final String MODULE_NAME_EN = "treenity_step3_dir";
    @Resource
    private CCFolderOpenService treenityCCFolderOpenService;

    @Override
    protected LoggerCollectionEnum getCollectionEnum() {
        return LoggerCollectionEnum.thirdStepDirCollection;
    }


    @ResponseBody
    @RequestMapping("/list")
    public RemoteResult<CourseFolderTreeDto> queryCourseFolderTree(@RequestParam(value = "courseId", required = false) final Long courseId) {
        RemoteResult<CourseFolderTreeDto> listRemoteResult = new ResultRequestAndParser<CourseFolderTreeDto>(MODULE_NAME_CN, MODULE_NAME_EN, "查询", "queryList") {
            @Override
            public RemoteResult request(Long curUserId) throws RemoteException {
//                RemoteResult<List<CourseChapterDto>> rs = treenityCourseChapterOpenService.queryListByCourseId(courseId);
                //调用接口获得——目录结构 数据
                RemoteResult<CourseFolderTreeDto> rs = treenityCCFolderOpenService.queryCourseFolderTree(courseId);
                return rs;
            }
        }.get();
        return listRemoteResult;
    }


    @ResponseBody
    @RequestMapping("/create")
    public RemoteResult<CourseFolderDto> chapterCreate(final CourseFolderDto courseFolderDto) {
        RemoteResult<CourseFolderDto> result = new ResultRequestAndParser<CourseFolderDto>(MODULE_NAME_CN, MODULE_NAME_EN, "创建", "create") {
            @Override
            public RemoteResult request(Long curUserId) throws RemoteException {
                RemoteResult<CourseFolderDto> rs = treenityCCFolderOpenService.createByDto(courseFolderDto, curUserId);
                return rs;
            }
        }.get();
        return result;
    }
//
    @ResponseBody
    @RequestMapping(value = "/delete",method = {RequestMethod.POST,RequestMethod.GET})
    public RemoteResult<Void> chapterDelete(@RequestParam(value = "folderIds[]", required = false) final List<Integer> folderIds) {
//        final List<CourseChapterDto> courseChapterDtos = stringToDtoList(sortList, CourseChapterDto.class);

        RemoteResult<Void> result = new ResultRequestAndParser<Void>(MODULE_NAME_CN, MODULE_NAME_EN, "删除", "delete") {
            @Override
            public RemoteResult request(Long curUserId) throws RemoteException {
                RemoteResult<Void> rs = treenityCCFolderOpenService.deleteByIds(folderIds, curUserId);
                return rs;
            }
        }.get();
        return result;
    }
//
    @ResponseBody
    @RequestMapping("/update")
    public RemoteResult<CourseFolderDto> chapterUpdate(final CourseFolderDto dto) {
        RemoteResult<CourseFolderDto> result = new ResultRequestAndParser<CourseFolderDto>(MODULE_NAME_CN, MODULE_NAME_EN, "更新", "update") {
            @Override
            public RemoteResult request(Long curUserId) throws RemoteException {
                RemoteResult<CourseFolderDto> rs = treenityCCFolderOpenService.updateAndGetByDto(dto, curUserId);
                return rs;
            }
        }.get();
        return result;
    }

    @ResponseBody
    @RequestMapping("/updateMulti")
    public RemoteResult<List<CourseFolderDto>> updateMulti(@RequestParam(value = "folderIds[]", required = false) final List<Integer> folderIds, Long parentId, final Integer privilege) {
        final List<CourseFolderDto> list = new ArrayList<CourseFolderDto>(folderIds.size());
        for (Integer fileId : folderIds) {
            CourseFolderDto one = new CourseFolderDto();
            one.setId(fileId);
            one.setParentId(parentId);
//            one.setPrivilege(privilege);//此处不做更新 另外做级联更新
            list.add(one);
        }

        RemoteResult<List<CourseFolderDto>> result = new ResultRequestAndParser<List<CourseFolderDto>>(MODULE_NAME_CN, MODULE_NAME_EN, "批量更新文件夹", "updateMulti") {
            @Override
            public RemoteResult request(Long curUserId) throws RemoteException {
                //更新所处文件夹
                RemoteResult<List<CourseFolderDto>> rs = treenityCCFolderOpenService.updateByDtoList(list, curUserId);

                //权限缩小时更新
                if(privilege != null && privilege == 1){
                    treenityCCFolderOpenService.updatePrivilageForAllChildren(folderIds,null,privilege,curUserId);
                }

                return rs;
            }
        }.get();
        return result;
    }

    @ResponseBody
    @RequestMapping("/updatePrivilage")
    public RemoteResult<Void> updateMulti(@RequestParam(value = "folderIds[]", required = false) final List<Integer> folderIds,
                                          @RequestParam(value = "fileIds[]", required = false) final List<Integer> fileIds,
                                          final Integer newPrivilage) {
        RemoteResult<Void> result = new ResultRequestAndParser<Void>(MODULE_NAME_CN, MODULE_NAME_EN, "批量更新权限", "updatePrivilage") {
            @Override
            public RemoteResult request(Long curUserId) throws RemoteException {
                RemoteResult<Void> rs = treenityCCFolderOpenService.updatePrivilageForAllChildren(folderIds,fileIds,newPrivilage, curUserId);
                return rs;
            }
        }.get();
        return result;
    }

}
